import sqlite3
import pandas as pd

def dataframe_to_sqlite(df, db_path, table_name):
    conn = sqlite3.connect(db_path)
    df.to_sql(table_name, conn, if_exists='replace', index=False)
    conn.close()

def sqlite_to_dataframe(db_path, table_name):
    conn = sqlite3.connect(db_path)
    df = pd.read_sql(f"SELECT * FROM {table_name}", conn)
    conn.close()
    return df

# Example usage:
# df = pd.DataFrame({
#     "filename": ["file1.txt", "file2.txt"],
#     "path": ["/path/to/file1", "/path/to/file2"],
#     "time": ["2023-01-01 10:00:00", "2023-01-02 11:00:00"],
#     "size": [1234, 5678]
# })
# dataframe_to_sqlite(df, 'example.db', 'files')
# new_df = sqlite_to_dataframe('example.db', 'files')
# print(new_df)

def add_record(db_path, table_name, record):
    conn = sqlite3.connect(db_path)
    placeholders = ', '.join(['?'] * len(record))
    columns = ', '.join(record.keys())
    sql = f"INSERT INTO {table_name} ({columns}) VALUES ({placeholders})"
    conn.execute(sql, list(record.values()))
    conn.commit()
    conn.close()

def delete_record(db_path, table_name, condition):
    conn = sqlite3.connect(db_path)
    sql = f"DELETE FROM {table_name} WHERE {condition}"
    conn.execute(sql)
    conn.commit()
    conn.close()

def query_records(db_path, table_name, condition=None):
    conn = sqlite3.connect(db_path)
    if condition:
        sql = f"SELECT * FROM {table_name} WHERE {condition}"
    else:
        sql = f"SELECT * FROM {table_name}"
    df = pd.read_sql(sql, conn)
    conn.close()
    return df

# Example usage:
# add_record('example.db', 'files', {"filename": "file3.txt", "path": "/path/to/file3", "time": "2023-01-03 12:00:00", "size": 91011})
# delete_record('example.db', 'files', "filename='file1.txt'")
# queried_df = query_records('example.db', 'files', "size > 5000")
# print(queried_df)